<template>
	<view>
		<u-cell-group>
			<u-cell-item  v-for="(item,index) in userList" :key='index'>
				<u-avatar   slot="icon" :src="item.avatar"></u-avatar>
				<text  slot='title'>
					{{item.username}}
				</text>
				<view  slot='right-icon'>
					<u-button :disabled="item.addRequest" type="success" size='mini' @click='addContact(item)'>添加</u-button>
				</view>
			</u-cell-item>
			
		</u-cell-group>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
import ChatBase from '@/common/mixins/chat_base.js'
export default {
	mixins:[ChatBase],
	data(){
		return{
			userList:[]
		}
	},
	onLoad:function(e){
		this.searchUser(e.serch)
	},
	methods:{
		searchUser(serch){
			const params={serch},self=this
			this.$request('login/searchUser',params)
				.then(res=>{
					const userList =res.userList ||{}
					if( userList.affectedDocs===0){
						uni.showModal({
							showCancel: false,
							content: '查询结果为空'
						})
						return
					}
					this.userList=userList.data.map(item=>{
						item.id=item._id
						return item
					})||[]
				}).catch(e=>console.log(e))
		},
		addContact(item){
			let send = this.userInfo.id,
				receive = item.id,
				message=item.nickname+'请求添加好友',
				self=this,
				data = JSON.stringify({controller:'User',method:'addRequest','data':{send,receive,message}});
			uni.sendSocketMessage({
				data,
				success:function(){
					self.$set(item,'addRequest',true)
					self.$refs.uToast.show({
						title: '好友申请发送成功',
						type: 'success',
					})
				}
			})
		}
	}
}
</script>

<style>
</style>
